﻿using DongFangGuoKai.WCSPlatform.WebApi.Extensions;
using DongFangGuoKai.WCSPlatform.WebApi.Utils;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace DongFangGuoKai.WCSPlatform.WebApi.Controllers
{
    /// <summary>
    /// WCS日志查询
    /// </summary>
    [Route("api/[controller]/[action]"), ApiController]
    public class LogController : ControllerBase
    {
        private readonly ISqlSugarClient client;

        public LogController(ISqlSugarClient client)
        {
            this.client = client;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="Level">等级</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<WCSApiResponse> GetLog(string? Level, DateTime? startTime, DateTime? stopTime, int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                var list = client.Queryable<Log>()
                            .WhereIF(!string.IsNullOrEmpty(Level), s => s.Level == Level)
                            .WhereIF(startTime != null && stopTime != null, s => s.Logged.Date >= startTime && s.Logged.Date <= stopTime).OrderByDescending(s=>s.Logged);


                var pageList = list.ToPageList(pageIndex, pageSize);

                return this.Success(new { pageList, count = list.Count() });
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<WCSApiResponse> DelLogById(int[]? ids)
        {
            try
            {
                var list = client.Queryable<Log>().Where(s=>ids.Contains(s.Id));
                var delRes = client.Deleteable<Log>(list).ExecuteCommand();

                return this.Success(delRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
        /// <summary>
        /// 删除全部
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<WCSApiResponse> DelLog()
        {
            try
            {
                var delRes = client.Deleteable<Log>().ExecuteCommand();

                return this.Success(delRes);
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }
    }


    /// <summary>
    /// 表示日志实体
    /// </summary>
    public class Log
    {
        [SugarColumn(IsPrimaryKey = true)]
        public int Id { get; set; }
        /// <summary>
        /// 获取或设置机器名称
        /// </summary>
        public string MachineName { get; set; }

        /// <summary>
        /// 获取或设置记录时间
        /// </summary>
        public DateTime Logged { get; set; }

        /// <summary>
        /// 获取或设置日志级别
        /// </summary>
        public string Level { get; set; }

        /// <summary>
        /// 获取或设置日志消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 获取或设置日志记录器
        /// </summary>
        public string Logger { get; set; }

        /// <summary>
        /// 获取或设置调用位置
        /// </summary>
        public string Callsite { get; set; }

        /// <summary>
        /// 获取或设置异常信息
        /// </summary>
        public string Exception { get; set; }
    }


}
